﻿package bipede.animation
{
	import bipede.controleurs.Animateur;
	import events.MoveEvent;
	import events.SequenceEvent;
	import flash.events.EventDispatcher;
	
	public class Sequenceur extends EventDispatcher
	{
		private var liste:Vector.<Animateur>;
		private var index:int=0;
		public var loop:Boolean;
		
		public function Sequenceur( liste:Vector.<Animateur>, loop:Boolean=false)
		{
			this.liste = liste;
			this.loop = loop;
		}
		public function start( repeatCount:int=0 ):void
		{
			play();
		}
		public function reset():void
		{
			index = 0;
		}
		public function stop():void
		{
		}		
		public function play():void
		{
			//trace("play_sequence ", index, liste[ index ] ,liste.length);
			liste[ index ].start();
			liste[ index ].addEventListener( SequenceEvent.END, next_sequence );			
		}		
		private function next_sequence(e:SequenceEvent):void 
		{
			liste[ index ].removeEventListener( SequenceEvent.END, next_sequence );
			index++;
			if ( index == liste.length )
			{
				if ( loop )
				{
					reset();
				}
				else {
					stop();
					return;
				}
			}
			trace("next_sequence ", index, liste[ index ] );
			play();
		}		
	}
}